package com.wsc.wisdomchinaqinghe.activitity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wsc.wisdomchinaqinghe.R;
import com.wsc.wisdomchinaqinghe.Tools.JsonTools;
import com.wsc.wisdomchinaqinghe.Tools.Tools;
import com.wsc.wisdomchinaqinghe.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchinaqinghe.application.MyApplication;
import com.wsc.wisdomchinaqinghe.callback.ContributorView;
import com.wsc.wisdomchinaqinghe.constant.Constant;
import com.wsc.wisdomchinaqinghe.javaBean.LivingExpenseBean;
import com.wsc.wisdomchinaqinghe.javaBean.RespObj;
import com.wsc.wisdomchinaqinghe.presenter.ContributorPresenter;
import com.wsc.wisdomchinaqinghe.view.MyToast;
import com.wsc.wisdomchinaqinghe.view.iphoneDialogBuilder;

/**
 * 生活缴费账户
 */
public class LivingExpensesCountActivity extends BaseActivity implements View.OnClickListener,ContributorView {

    private TextView fragment_lyhd_title;//标题
    private ImageView fragment_lyhd_back;//返回按钮
    private Context context;
    private String itemId;
    private String projectId;
    private ContributorPresenter contributorPresenter;
    private final String HOMEITEMLISTFLAG="HOMEITEMLISTFLAG";
    private LinearLayout layout_see;
    private Button bt_yuyue;
    private TextView tv_danwei;
    private EditText et_huhao;
    private MyToast myToast;
    private String cidNumber;
    private MyApplication myApplication;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.liveexpensecount_layout);
        context=this;
        myToast=new MyToast(context);
        myApplication=(MyApplication) getApplication();
        contributorPresenter = new ContributorPresenter(this, context);
        Intent intent=getIntent();
        itemId=intent.getStringExtra("itemId");
        projectId=intent.getStringExtra("projectId");
        cidNumber=intent.getStringExtra("cidNumber");
        initView();
    }

    private void initView() {
        fragment_lyhd_title=(TextView) findViewById(R.id.tv_headtitle);//标题
        fragment_lyhd_back=(ImageView) findViewById(R.id.regist_back);//返回按钮
        fragment_lyhd_back.setOnClickListener(this);
        fragment_lyhd_title.setText("缴费账户");
        contributorPresenter.getHomeItemName(projectId,itemId,HOMEITEMLISTFLAG);
        layout_see=(LinearLayout) findViewById(R.id.layout_see);
        layout_see.setOnClickListener(this);
        bt_yuyue=(Button) findViewById(R.id.bt_yuyue);
        bt_yuyue.setOnClickListener(this);
        tv_danwei=(TextView) findViewById(R.id.tv_danwei);
        et_huhao=(EditText)findViewById(R.id.et_huhao);
    }

    @Override
    public void onLoadContributorStart(String flag) {

    }

    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            //banner
            if (flag.equals(HOMEITEMLISTFLAG)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String jsonMessage = JsonTools.objectToJsonString(object);
                    LivingExpenseBean livingExpenseBean=JsonTools.jsonStringToObject(jsonMessage,LivingExpenseBean.class);
                    tv_danwei.setText(livingExpenseBean.getItemName());
                }else
                {

                }

            }

        } catch (Exception e) {

        }
    }

    @Override
    public void onLoadContributorComplete(String flag) {
    }

    @Override
    public void onLoadContributorError(String error, String flag) {
    }


    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.regist_back:
                finish();
                break;
            case R.id.bt_yuyue:
                String huhao=et_huhao.getText().toString().trim();
                String danwei=tv_danwei.getText().toString().trim();
                boolean isLogin= Tools.getSharedPreferencesboolen(Constant.isLoginIn,false,context);
                if(isLogin)
                {
                    if(huhao!=null && !huhao.isEmpty() && danwei!=null && !danwei.isEmpty())
                    {
                        Intent intent=new Intent(context,LivingExpensesDetailActivity.class);
                        intent.putExtra("count",huhao);
                        intent.putExtra("itemId",itemId);
                        intent.putExtra("projectId",projectId);
                        intent.putExtra("cidNumber",cidNumber);
                        startActivity(intent);
                    }else {
                        if(huhao==null || huhao.isEmpty())
                        {
                            myToast.showShort("户号不能为空");
                        }
                        if(danwei==null || danwei.isEmpty())
                        {
                            myToast.showShort("缴费单位不能为空");
                        }
                    }
                }else
                {
                    iphoneDialogBuilder idb = new iphoneDialogBuilder(context);
                    idb.setTitle("提示");
                    idb.setMessage("是否登录");
                    idb.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent(context, Login_RegisterActivity.class);
                            myApplication.setSourceIntoLoginRegisterActivity(LivingExpensesCountActivity.class.getSimpleName());
                            startActivity(intent);

                        }
                    });
                    idb.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                    idb.show();
                }
                break;
            //协议
            case R.id.layout_see:
                Intent intent=new Intent(context,ExpensesProcontrolActivity.class);
                startActivity(intent);
                break;
        }
    }

}
